package com.ruicar.afs.cloud.finance.common.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.modules.contract.enums.UnusedFundsStatusEnums;
import com.ruicar.afs.cloud.common.mq.finance.debit.dto.StartReceiptHandleDto;
import com.ruicar.afs.cloud.finance.common.entity.FinanceCustUnusedFunds;
import com.ruicar.afs.cloud.finance.common.mapper.FinanceCustUnusedFundsMapper;
import com.ruicar.afs.cloud.finance.common.service.FinanceCustUnusedFundsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @author: Lee
 * @date 2020-05-30 04:55:43 
 * @description 
 */
@Service
@Slf4j
public class FinanceCustUnusedFundsServiceImpl extends ServiceImpl<FinanceCustUnusedFundsMapper, FinanceCustUnusedFunds> implements FinanceCustUnusedFundsService {

    @Autowired
    private FinanceCustUnusedFundsMapper financeCustUnusedFundsMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean startReceiptHandleErrorHandle(String message) {
        JSONObject content = JSONObject.parseObject(JSONObject.parseObject(message).getString("content"));
        StartReceiptHandleDto startReceiptHandleDto = content.getJSONObject("data").toJavaObject(StartReceiptHandleDto.class);
        if(startReceiptHandleDto != null
                && startReceiptHandleDto.getIdList() != null
                    && startReceiptHandleDto.getIdList().size()>0){
            /**  发送失败，修改状态 by ZC.GUO  **/
            for (Long id : startReceiptHandleDto.getIdList()){
                boolean flag = false;
                while(!flag){
                    FinanceCustUnusedFunds financeCustUnusedFunds = this.getOne(Wrappers.<FinanceCustUnusedFunds>query().lambda()
                            .eq(FinanceCustUnusedFunds::getFundId,id)
                            .eq(FinanceCustUnusedFunds::getStatus, UnusedFundsStatusEnums.locked)
                    );
                    if (financeCustUnusedFunds != null){
                        financeCustUnusedFunds.setStatus(UnusedFundsStatusEnums.mayuse);
                        this.updateWhenStartDistribution(financeCustUnusedFunds);
                        flag = true;
                    }
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public void updateWhenDistribution(FinanceCustUnusedFunds custUnusedFunds, BigDecimal amount) {

        int num = financeCustUnusedFundsMapper.updateWhenDistribution(amount,UnusedFundsStatusEnums.mayuse,custUnusedFunds.getId(),custUnusedFunds.getRevision());

        if (num < 1){
            log.warn("乐观锁生效，数据更新失败，待分配款项id为：{}，乐观锁值为：{}",custUnusedFunds.getId(),custUnusedFunds.getRevision());
            throw new AfsBaseException("乐观锁生效，数据更新失败，待分配款项id为："+custUnusedFunds.getId());
        }
    }

    @Override
    public boolean updateWhenStartDistribution(FinanceCustUnusedFunds custUnusedFunds) {

        int num = financeCustUnusedFundsMapper.updateWhenStartDistribution(custUnusedFunds.getStatus(),custUnusedFunds.getId(),custUnusedFunds.getRevision());

        if (num < 1){
            log.info("乐观锁生效，数据更新失败，待分配款项id为：{}，乐观锁值为：{}",custUnusedFunds.getId(),custUnusedFunds.getRevision());
            return false;
        }else{
            return true;
        }
    }
}
